﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ScrumDashboard.Library.Model
{
    public class SprintViewData
    {
        public SprintItem Sprint { get; set; }
        public SprintViewStatistics Statistics { get; set; }
        public IList<SprintBacklogItem> SprintBacklog { get; set; }
        public IList<ProductBacklogItem> ProductBacklog { get; set; }
    }

    public class SprintViewStatistics
    {
        public static SprintViewStatistics Empty = new SprintViewStatistics();

        private SprintViewStatistics()
        {
        }

        public static SprintViewStatistics ComputeStatistics(SprintViewData model)
        {
            SprintViewStatistics stats = new SprintViewStatistics();

            foreach (ProductBacklogItem pbi in model.ProductBacklog)
            {
                stats.EstimatedEffort += pbi.EstimatedEffort;
                if (pbi.State == "Done")
                    stats.CurrentEffort += pbi.EstimatedEffort;
            }

            if (stats.CurrentEffort > 0)
                stats.CurrentEffortPercent = 100 * stats.CurrentEffort / stats.EstimatedEffort;
            else
                stats.CurrentEffortPercent = 0;

            stats.WorkTotal = 0;
            stats.WorkRemaining = 0;
            foreach (SprintBacklogItem sbi in model.SprintBacklog)
            {
                stats.WorkRemaining += sbi.WorkRemaining;
                stats.WorkTotal += sbi.EstimatedEffort;
                if (sbi.IsUnplanned(model.Sprint.SprintStart))
                    stats.ItemsUnplannedWork += sbi.EstimatedEffort;

                switch (sbi.State)
                {
                    case "Done":
                        stats.ItemsDone += sbi.EstimatedEffort;
                        break;
                    case "In Progress":
                        stats.ItemsInProgress += sbi.WorkRemaining;
                        break;
                    case "Ready For Test":
                        stats.ItemsReadyForTest += sbi.WorkRemaining;
                        break;
                    case "Not Done":
                        stats.ItemsNotDone += sbi.EstimatedEffort;
                        break;
                }
            }

            stats.CompletedWork = stats.WorkTotal - stats.WorkRemaining;

            if (stats.WorkTotal > 0)
                stats.CompletedWorkPercent = 100 * stats.CompletedWork / stats.WorkTotal;
            else
                stats.CompletedWorkPercent = 0;

            return stats;
        }

        public double CompletedWork;
        public double CompletedWorkPercent;
        public double WorkTotal;
        public double WorkRemaining;

        public double CurrentEffort;
        public double CurrentEffortPercent;
        public double EstimatedEffort;

        public double ItemsDone;
        public double ItemsNotDone;
        public double ItemsReadyForTest;
        public double ItemsInProgress;
        public double ItemsUnplannedWork;
    }

}
